﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Stores.Models;
using Microhard.Plat.CRM.Stores.DataAccess;
using Microhard.Plat.CRM.Stores.Msg;
using Microhard.Plat.CRM.Stores.DTO;
using Microhard.Plat.CRM.Stores.Interface;


namespace Micrhard.FMCG.Server.Impl
{
    public class IspTemplateAnswerImpl : IIspTemplateAnswer
    {
        public bool IsNameDuplicate(IspTemplateAnswerModel model)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var cnt = db.CheckListTemplateAnswers.Count(c => c.ItemID == model.ItemID && c.Comment == model.Comment);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(IspTemplateAnswerModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var op = model.ToEntity();
            try
            {
                op.CheckListAnswerID = Guid.NewGuid();
                op.CreateTime = DateTime.Now;
                db.CheckListTemplateAnswers.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, IspTemplateAnswerMsg.AddFailed);
                Log.Error("IspTemplateAnswer.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(IspTemplateAnswerModel model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.CheckListTemplateAnswers.SingleOrDefault(l => l.CheckListAnswerID == model.CheckListAnswerID);
            if (entity != null)
            {
                entity.Comment = model.Comment;
                entity.Value = model.Value;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, IspTemplateAnswerMsg.UpdateFailed);
                Log.Error("IIspTemplateAnswer.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.CheckListTemplateAnswers.Where(l => ids.Contains(l.CheckListAnswerID));
            foreach (var e in entity)
            {
                //模板是否在使用
                if (e.CheckListSheetAnswers.Count == 0)
                {
                    db.CheckListTemplateAnswers.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(IspTemplateAnswerMsg.NameIsInUsing, e.Comment));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, IspItemMsg.DeleteFailed);
                    Log.Error("IIspCategory.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(IspTemplateAnswerModel model)
        {
            throw new NotImplementedException();
        }

        public IspTemplateAnswerModel GetModelByID(Guid id)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var mcmp = db.CheckListTemplateAnswers.SingleOrDefault(c => c.CheckListAnswerID == id);
            if (mcmp != null)
            {
                return mcmp.ToModel();
            }
            return null;
        }

        public IspTemplateAnswerModel GetModelByModel(IspTemplateAnswerModel model)
        {
            throw new NotImplementedException();
        }

        public List<IspTemplateAnswerModel> Query(IspTemplateAnswerModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();

            IEnumerable<CheckListTemplateAnswer> entities = from b in db.CheckListTemplateAnswers
                                                            where b.ItemID == Model.ItemID
                                                            select b;

            List<IspTemplateAnswerModel> entity = new List<IspTemplateAnswerModel>();

            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
            {
                return pagedEntities.Select(m => m.ToModel()).ToList();
            }
            return new List<IspTemplateAnswerModel>();
        }

        public List<IspTemplateAnswerModel> Query(IspTemplateAnswerModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }




    }
}
